{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "provincial-explanation",
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <regex>\n",
    "#include <iostream>\n",
    "#include <string>\n",
    "\n",
    "using namespace std;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "vocational-september",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  yingshaoxo@\n",
      "  gmail\n",
      "  com\n"
     ]
    }
   ],
   "source": [
    "string s = \"yingshaoxo@gmail.com\";\n",
    "regex word_regex(\"(@*\\\\w+@*)\");\n",
    "auto words_begin = std::sregex_iterator(s.begin(), s.end(), word_regex);\n",
    "auto words_end = std::sregex_iterator();\n",
    "\n",
    "for (std::sregex_iterator i = words_begin; i != words_end; ++i) {\n",
    "    std::smatch match = *i;\n",
    "    std::string match_str = match.str();\n",
    "    std::cout << \"  \" << match_str << '\\n';\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "macro-copying",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hisadf\n"
     ]
    }
   ],
   "source": [
    "string s = \"hi.sadf\";\n",
    "regex regexp(\"\\\\.\");\n",
    "cout << regex_replace(s, regexp, \"\") << endl;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "joined-hammer",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/unique-email-addresses\n",
    "\n",
    "Runtime: 128 ms, faster than 5.07% of C++ online submissions for Unique Email Addresses.\n",
    "Memory Usage: 55.8 MB, less than 5.01% of C++ online submissions for Unique Email Addresses.\n",
    "\n",
    "```cpp\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <string>\n",
    "#include <set>\n",
    "#include <regex>\n",
    "#include <iostream>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "class Solution\n",
    "{\n",
    "public:\n",
    "    int numUniqueEmails(vector<string> &emails)\n",
    "    {\n",
    "        //7:45\n",
    "        set<string> real_emails;\n",
    "        for (const string &email : emails)\n",
    "        {\n",
    "            auto splits = split(email, \"@\");\n",
    "            auto local_name = splits[0];\n",
    "            auto domain_name = splits[1];\n",
    "            local_name = remove_dot(local_name);\n",
    "            splits = split(local_name, \"+\");\n",
    "            local_name = splits[0];\n",
    "            //cout << local_name + \"@\" + domain_name << endl;\n",
    "            real_emails.insert(local_name + \"@\" + domain_name);\n",
    "        }\n",
    "        return real_emails.size();\n",
    "        //8:47, it's funny that I can't use regex to split a string\n",
    "    }\n",
    "    vector<string> split(string s, string splitor)\n",
    "    {\n",
    "        vector<string> list;\n",
    "        size_t pos = 0;\n",
    "        string token;\n",
    "        while ((pos = s.find(splitor)) != string::npos)\n",
    "        {\n",
    "            token = s.substr(0, pos);\n",
    "            list.push_back(token);\n",
    "            s.erase(0, pos + splitor.length());\n",
    "        }\n",
    "        list.push_back(s);\n",
    "        return list;\n",
    "    }\n",
    "    string remove_dot(string s)\n",
    "    {\n",
    "        regex regexp(\"\\\\.\");\n",
    "        return regex_replace(s, regexp, \"\");\n",
    "    }\n",
    "};\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
